package com.fingard.dsp.bank.directbank.dlcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 大连银行单笔支付
 * 
 * 参考文档：大连银行银企直联企业对接交易接口(部分)v1.0.2.docx
 * 		
 * @author Cruise
 * @date 2018/11/06
 */
public class DLCB01SinglePay extends DLCB01Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = null;
		if ("190801".equals(p_req1908.reqHeader.commandCode)) {
			// tmpRetDetail = d_190801(p_req1908);
			return null;
		} else {
			tmpRetDetail = d_190802(p_req1908);
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	/**
	 * 对外支付
	 * 
	 * 支持接口: 2.2.1 对外转账(对公) �C416B300001 2.2.3 对私转账 �C416B301001
	 * 
	 * @param p_req1908
	 * @return
	 * @throws Exception
	 */
	public RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;

		try {
			DLCB01Header tmpBankHeader = new DLCB01Header();
			if ("1".equals(p_req1908.singleDetail.isPrivate)) {
				tmpBankHeader.trncode = "416B301001";
			} else {
				tmpBankHeader.trncode = "416B300001";
			}
			tmpBankHeader.reqSeqNo = p_req1908.reqHeader.reqSeqID;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, tmpBankHeader);

			XElement tmpXElement = new XElement("body", "");
			tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));
			tmpXElement.addChild(new XElement("pay_cur_code", "01"));
			tmpXElement.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));
			tmpXElement.addChild(new XElement("as_flag", "0"));
			tmpXElement.addChild(new XElement("as_acno", ""));
			tmpXElement.addChild(new XElement("as_acname", ""));
			tmpXElement.addChild(new XElement("pay_accaddr", ""));
			tmpXElement.addChild(new XElement("cert_type", ""));
			tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));
			tmpXElement.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));
			tmpXElement.addChild(new XElement("rcv_cur_code", "01"));
			tmpXElement.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));
			tmpXElement.addChild(new XElement("rcv_accaddr", ""));
			tmpXElement.addChild(new XElement("rcv_bank_no", p_req1908.singleDetail.cnapsCode));
			tmpXElement.addChild(new XElement("rcv_bank_name", p_req1908.singleDetail.cnapsName));
			tmpXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));
			String sameBnk = "1".equals(p_req1908.singleDetail.sameBnk) ? "0" : "1";
			tmpXElement.addChild(new XElement("bank_flag", sameBnk));
			tmpXElement.addChild(new XElement("urgency_flag", p_req1908.singleDetail.paySpd));
			String sameCity = "1".equals(p_req1908.singleDetail.sameCity) ? "0" : "1";
			tmpXElement.addChild(new XElement("area_flag", sameCity));
			tmpXElement.addChild(new XElement("mobiles", ""));
			tmpXElement.addChild(new XElement("purpose", p_req1908.singleDetail.abstractATS));
			tmpXElement.addChild(new XElement("postscript", p_req1908.singleDetail.abstractATS));
			tmpXElement.addChild(new XElement("booking_flag", "0"));
			tmpXElement.addChild(new XElement("booking_date", ""));
			tmpXElement.addChild(new XElement("booking_time", ""));
			tmpXElement.addChild(new XElement("saverecvinfo_flag", "0"));
			tmpXElement.addChild(new XElement("delay_flag", "0"));

			if (!"1".equals(p_req1908.singleDetail.isPrivate)) {
				tmpXElement.addChild(new XElement("vir_acno", ""));
				tmpXElement.addChild(new XElement("vir_acname", ""));
			}

			nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpSuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
					String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
					String tmpRetMsg=	tmpBkReader.getFirstText("/ap/head/ret_info");
					String tmpExtMsg = tmpBkReader.getFirstText("/ap/head/ext_info");
					tmpRetDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetMsg, tmpExtMsg);
					tmpRetDetail.respCode = "0";
					tmpRetDetail.transState = StringHelper.joinNotEmpty("+", tmpSuccFlag, tmpRetCode);
					tmpRetDetail.payInfoCode = tmpRetCode;
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	/**
	 * 单笔支付查询
	 * 
	 * 支持接口： 2.2.7 对外转账结果查询(适用对公对私) -416B3011
	 */
	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
		tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;

		try {
			DLCB01Header tmpBankHeader = new DLCB01Header();
			tmpBankHeader.trncode = "416B3011";
			tmpBankHeader.reqSeqNo = p_req1944.reqHeader.transSeqID;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, tmpBankHeader);

			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("serial_no", p_req1944.askNumber);
			xmlWriter.writeElementString("tr_acdt", Format.Date8Format.format(p_req1944.getBgnDateTime()));
			xmlWriter.writeElementString("Reserved", "");
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpSuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
					String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
					String tmpRetMsg = tmpBkReader.getFirstText("/ap/head/ret_info");
					if ("0".equals(tmpSuccFlag) && "0000".equals(tmpRetCode)) {
						tmpRetAtsDetail.respCode = "0";
						tmpRetAtsDetail.respInfo = tmpRetMsg;
						tmpRetAtsDetail.transState = StringHelper.joinNotEmpty("+", tmpSuccFlag, tmpBkReader.getFirstText("/ap/body/stat"));
						tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
					} else {
						String extInfo = tmpBkReader.getFirstText("/ap/head/ext_info"); 
						if ("-999".equals(tmpRetCode) && extInfo.contains("流水号不存在")) {
							if (canBeNotExist(p_req1944, tmpRetAtsDetail, 45)) {
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = extInfo;
								tmpRetAtsDetail.transState = StringHelper.joinNotEmpty("+", tmpRetCode, extInfo);
								tmpRetAtsDetail.payInfoCode = extInfo;
							}
						} else {
							tmpRetAtsDetail.respCode = "-1";
							tmpRetAtsDetail.respInfo = tmpRetMsg + "+" + extInfo;
						}
					}
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
